﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using EIS.DatabaseHelper;
using EIS.Obj;


namespace EIS.ClientUtility
{
    public sealed class ClientConfig
    {
        private static readonly ClientConfig instance = new ClientConfig();
        private int _heartBeatInterval;
        public int DisplayDaysForFunctionDate = 30;

        public int RefreshNavBarTextAndStyle = 5000;

        public bool RefreshViewAfterNewRecordComingIn = true;
        public bool MarkReadAfterSelectRow = false;
        public LogonInfo LogonInfo = new LogonInfo();
        private int _fetchTransactionDataInterval;

        public int HeartBeatInterval
        {
            get { return _heartBeatInterval; }
            set { _heartBeatInterval = value; }
        }

        private ClientConfig()
        {

            var dbHandler = new DBHandler();
            DataSet ds = dbHandler.SelectSimple(EISConstant.ClientCacheTableName);

            //HeartBeatInterval, Fixed Value
            _heartBeatInterval = 10000;
            _fetchTransactionDataInterval = 10000;
            //Logon
            


            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count != 0)
            {
                LogonInfo.UserName = ds.Tables[0].Rows[0]["LogonUserName"].ToString();
                LogonInfo.Password = ds.Tables[0].Rows[0]["LogonUserPassword"].ToString();
                LogonInfo.StoreUsername = ds.Tables[0].Rows[0]["SaveLogonUserName"].ToString() == "1";
                LogonInfo.StorePassword = ds.Tables[0].Rows[0]["SaveLogonUserPassword"].ToString() == "1";

            }
            else
            {
                var dr = ds.Tables[0].NewRow();
                dr["ID"] = 0;
                ds.Tables[0].Rows.Add(dr);
                dbHandler.InsertRecords(EISConstant.ClientCacheTableName, ds.Tables[0]);
            }

            ReadBasicConfig();

        }

        public void UpdateConfig()
        {
            var dbHandler = new DBHandler();
            DataSet ds = dbHandler.SelectSimple(EISConstant.ClientCacheTableName);

            //Logon

            ds.Tables[0].Rows[0]["ID"] = 0;
            ds.Tables[0].Rows[0]["SaveLogonUserName"] = LogonInfo.StoreUsername ? "1" : "0";
            ds.Tables[0].Rows[0]["SaveLogonUserPassword"] = LogonInfo.StorePassword ? "1" : "0";

            if (LogonInfo.StoreUsername)
            {
                ds.Tables[0].Rows[0]["LogonUserName"] = LogonInfo.UserName;
            }
            else
            {
                ds.Tables[0].Rows[0]["LogonUserName"] = String.Empty;
            }

            if (LogonInfo.StorePassword)
            {
                ds.Tables[0].Rows[0]["LogonUserPassword"] = LogonInfo.Password;
            }
            else
            {
                ds.Tables[0].Rows[0]["LogonUserPassword"] = String.Empty;
            }

            dbHandler.UpdateRecords(EISConstant.ClientCacheTableName, ds.Tables[0]);
        }

        public void UpdateBasicConfig() 
        {
            var dbHandler = new DBHandler();
            DataSet ds = dbHandler.SelectSimple(EISConstant.ClientConfigTableName);
            ds.Tables[0].Rows.Clear();
            
            var dr = ds.Tables[0].NewRow();
            dr["Variable"] = "DisplayDaysForFunctionDate";
            dr["Value"] = this.DisplayDaysForFunctionDate;
            ds.Tables[0].Rows.Add(dr);
            
            dr = ds.Tables[0].NewRow();
            dr["Variable"] = "FetchTransactionDataInterval";
            dr["Value"] = this.FetchTransactionDataInterval;
            ds.Tables[0].Rows.Add(dr);

            dr = ds.Tables[0].NewRow();
            dr["Variable"] = "MarkReadAfterSelectRow";
            dr["Value"] = MarkReadAfterSelectRow ? 1 : 0;
            ds.Tables[0].Rows.Add(dr);

            /*
            dr = ds.Tables[0].NewRow();
            dr["Variable"] = "RefreshViewAfterNewRecordComingIn";
            dr["Value"] = RefreshViewAfterNewRecordComingIn ? 1 : 0;
            ds.Tables[0].Rows.Add(dr);
            */


            dbHandler.UpdateRecords(EISConstant.ClientConfigTableName, ds.Tables[0]);
        }

        public void ReadBasicConfig()
        {
            var dbHandler = new DBHandler();
            DataSet ds = dbHandler.SelectSimple(EISConstant.ClientConfigTableName);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["Variable"].ToString() == "FetchTransactionDataInterval")
                {
                    this.FetchTransactionDataInterval = Int16.Parse(dr["Value"].ToString());
                }
                if (dr["Variable"].ToString() == "MarkReadAfterSelectRow")
                {
                    this.MarkReadAfterSelectRow = dr["Value"].ToString() == "1";
                }
                if (dr["Variable"].ToString() == "DisplayDaysForFunctionDate")
                {
                    this.DisplayDaysForFunctionDate = Int16.Parse(dr["Value"].ToString());
                }
            }
        }
        public static ClientConfig Instance
        {
            get
            {
                return instance;
            }
        }

        public int FetchTransactionDataInterval
        {
            get { return _fetchTransactionDataInterval; }
            set { _fetchTransactionDataInterval = value; }
        }

        public List<string> GetMaintainedLayout()
        {
            DBHandler dbHandler = new DBHandler();
            DataSet ds = dbHandler.SelectSimple(EISConstant.ClientLayoutTableName);
            var layouts = new List<string>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if ( dr["FunctionID"].ToString() == GlobalVariable.Instance.CurrentFunctionID)
                {
                    layouts.Add(dr["Layout"].ToString());
                }
            }
            return layouts;
        }
    }

}
